<template>
  <div class="container">
    <el-card>
      <el-form ref="form" inline :model="formList">
        <el-form-item label="关键字">
          <el-input
            placeholder="请输入"
            v-model="formList.keyword"
            size="mini"
          ></el-input>
        </el-form-item>

        <el-form-item label="状态">
          <el-select
            style="width: 150px"
            placeholder="请输入"
            v-model="formList.state"
            size="mini"
          >
            <el-option value="0" label="禁用"> </el-option>
            <el-option value="1" label="启用"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" @click="resetForm">清除</el-button>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-search"
            @click="getArticlesList"
            >搜索</el-button
          >
        </el-form-item>
        <el-button
          style="float: right"
          @click="addButton"
          icon="el-icon-edit"
          type="success"
          size="small"
          >新增技巧</el-button
        >
      </el-form>

      <!-- 信息提示组件 -->
      <el-alert
        :title="`共${total}条记录`"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      ></el-alert>

      <!-- table 组件 -->
      <el-table :data="tableList" border style="width: 100%">
        <el-table-column type="index" label="序号" align="center" width="120">
        </el-table-column>
        <el-table-column label="文章标题" align="center">
          <template slot-scope="scope">
            {{ scope.row.title }}
            <!-- <i
              v-if="scope.row.videoURL"
              class="el-icon-film"
              style="color: blue; cursor: pointer"
            /> -->
            <i
              style="cursor: pointer"
              v-if="scope.row.videoURL"
              @click="playVideo(scope.row.videoURL)"
              ref="btn"
              :class="
                scope.row.videoURL ? 'el-icon-film' : 'el-icon-video-pause'
              "
              :style="scope.row.videoURL ? 'color: blue;' : ''"
            >
            </i>
          </template>
        </el-table-column>
        <el-table-column
          prop="visits"
          label="阅读数"
          align="center"
          width="120"
        >
        </el-table-column>
        <el-table-column
          prop="username"
          label="录入人"
          align="center"
          width="120"
        >
        </el-table-column>
        <el-table-column prop="createTime" label="录入时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.createTime | formatDate }}
          </template>
        </el-table-column>
        <el-table-column width="130" prop="state" label="状态" align="center">
          <template slot-scope="scope">
            {{ scope.row.state === 0 ? "已禁用" : "已启用" }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="220">
          <template slot-scope="scope">
            <button class="btnn" @click="preview(scope.row)">预览</button>
            <button class="btnn" @click="changeState(scope.row)">
              {{ scope.row.state == 1 ? "禁用" : "启用" }}
            </button>
            <button
              class="btnn"
              @click="updateArticles(scope.row)"
              :disabled="scope.row.state == 1 ? true : false"
              :class="{ jinzhi: scope.row.state == 1 ? true : false }"
            >
              修改
            </button>
            <button
              class="btnn"
              @click="delArticles(scope.row)"
              :class="{ jinzhi: scope.row.state == 1 ? true : false }"
              :disabled="scope.row.state == 1 ? true : false"
            >
              删除
            </button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-row type="flex" justify="end" style="margin-top: 10px">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage4"
            :page-sizes="[3, 5, 8, 10]"
            :page-size="formList.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </el-row>
      </div>
    </el-card>
    <ArticlesPreview :showDialog.sync="showDialog" :content="content" />
    <ArticlesAdd
      ref="add"
      :showDialogAtr.sync="showDialogAtr"
      @addReady="getArticlesList"
    />
    <!-- 视频 -->
    <el-dialog
      title="视频预览"
      :visible.sync="dialogPlay"
      width="30%"
      @close="closeDialog"
    >
      <video
        :src="videoUrl"
        controls
        autoplay
        class="video"
        ref="dialogVideo"
        width="100%"
      ></video>
    </el-dialog>
  </div>
</template>

<script>
import { articlesList, removeArticle, changeState } from "@/api/hmmm/articles";
import ArticlesPreview from "../components/articles-preview";
import ArticlesAdd from "../components/articles-add";
export default {
  components: {
    ArticlesPreview,
    ArticlesAdd,
  },
  data() {
    return {
      formList: {
        // 这个是用来发请求用的
        page: 1,
        pagesize: 10,
        keyword: "",
      },
      total: 0,
      tableList: [],
      currentPage4: 1,
      showDialog: false,
      content: {},
      showDialogAtr: false,
      dialogPlay: false,
      videoUrl: "",
    };
  },
  created() {
    this.getArticlesList();
  },
  methods: {
    // 视频
    playVideo(url) {
      this.dialogPlay = true;
      this.videoUrl = url;
    },

    // 改变每页条数功能
    handleSizeChange(val) {
      this.formList.pagesize = val;
      this.formList.page = 1
      this.getArticlesList();
    },

    // 改变分页功能
    handleCurrentChange(val) {
      this.formList.page = val;
      this.getArticlesList();
    },

    // 初始化渲染功能
    async getArticlesList() {
      const res = await articlesList(this.formList);
      this.tableList = res.data.items;
      this.total = res.data.counts;
    },

    // 重置功能
    resetForm() {
      this.formList = {
        // 这个是用来发请求用的
        page: 1,
        pagesize: 10,
        keyword: "",
      };
      this.getArticlesList();
    },

    // 新增功能
    addButton() {
      this.showDialogAtr = true;
    },

    // 删除功能
    async delArticles(data) {
      await this.$confirm("是否要删除？");
      const res = await removeArticle(data);
      this.$message({
        type: "success",
        message: "删除成功！",
      });
      this.getArticlesList();
    },

    // 修改状态
    async changeState(data) {
      data.state = data.state == 0 ? 1 : 0;
      const res = await changeState(data);
      this.$message({
        type: "success",
        message: "修改成功！",
      });
    },

    // 预览功能
    preview(data) {
      this.content = data;
      this.showDialog = true;
    },
    closeDialog() {
      this.dialogPlay = false;
    },
    updateArticles(data) {
      this.showDialogAtr = true;
      this.$refs.add.getDetailArticle(data);
    },
  },
};
</script>

<style scoped>
.container {
  padding: 5px 10px;
}
.alert {
  margin-bottom: 15px;
}
.btnn {
  margin-right: 5px;
  color: rgb(27, 187, 240);
  cursor: pointer;
  border: none;
  background: none;
}
.jinzhi {
  color: rgb(123, 123, 122);
  cursor: no-drop;
}
</style>
